package com.robooot.business_counter.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.robooot.business_counter.dao.CourierDao;
import com.robooot.business_counter.entity.Courier;
import com.robooot.business_counter.entity.SmsConfig;
import com.robooot.business_counter.exception.BizException;
import com.robooot.business_counter.model.CourierVo;
import com.robooot.business_counter.util.JsonUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.robooot.business_counter.enums.ConfigEnum.SMS_COMMON_CONFIG;

@Service
public class CourierService {
    @Autowired
    private CourierDao courierDao;
    @Autowired
    private ConfigService configService;

    /**
     * 添加邮递员
     * @param courierVo
     */
    @Transactional
    public void addCourier(CourierVo courierVo) {
        courierVo.setCourierId(UUID.randomUUID().toString().replaceAll("-",""));
        JSONObject courierInfo = courierDao.getCourierInfo(courierVo.getPhone());
        if(null == courierInfo){
            courierDao.addCourier(courierVo);
        }else {
            throw new BizException("改快递员手机号已绑定,请更换手机号");
        }

    }

    /**
     * 修改邮递员
     * @param courierVo
     */
    @Transactional
    public void updateCourier(CourierVo courierVo) {
        if (StringUtils.isBlank(courierVo.getPhone())) {
            courierDao.updateCourier(courierVo);
            return;
        }
        JSONObject courierInfo = courierDao.getCourierInfo(courierVo.getPhone());
        if(null == courierInfo){
            courierDao.updateCourier(courierVo);
        }else {
            throw new BizException("改快递员手机号已绑定,请更换手机号");
        }
    }

    /**
     * 删除邮递员
     * @param courierId
     */
    public void deleteCourier(String courierId) {
        courierDao.deleteCourier(courierId);
    }

    /**
     * 查看所有邮递员列表
     * @param courierVo
     */
    public List<Courier> getCourierList(CourierVo courierVo) {
       return courierDao.getCourierList(courierVo);
    }


    //以下都是发短信操作
    public void sendMessages(String userPhone, String counterNumber, String deviceName,String courierPhone,String address) {
        HashMap<Object, Object> map = new HashMap<>();
        //map.put("code","您有一个快递已帮您放到快递柜,请尽快打开闪易app查看详情");
        map.put("vendName",deviceName);
        map.put("boxNumber",counterNumber);
        map.put("phone",courierPhone);
        map.put("location",address);
        CommonRequest commonRequest = getSmsCommonConfig("001");
        SmsConfig smsConfig = courierDao.getSmsByType();
        commonRequest.putQueryParameter("PhoneNumbers",userPhone);
        commonRequest.putQueryParameter("TemplateParam", JsonUtils.getJsonFromObject(map));
        commonRequest.putQueryParameter("TemplateCode",smsConfig.getTemplateCode());
        commonRequest.putQueryParameter("SignName",smsConfig.getSignName());
        try {
            sendMessage(commonRequest,"001");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    private void sendMessage(CommonRequest commonRequest, String providerId) throws ClientException {
        IAcsClient iAcsClient = getIAcsClient(providerId);
        CommonResponse response = iAcsClient.getCommonResponse(commonRequest);
    }

    private Map<String, String> getCommonSmsConfig(String providerId) {
        Map<String, String> result = configService.getConfig(SMS_COMMON_CONFIG.getName(),providerId);
        return result;
    }

    private IAcsClient getIAcsClient(String providerId) {
        Map<String, String> smsConfig = getCommonSmsConfig(providerId);
        String accessKeyId = smsConfig.get("accessKeyId");
        String accessKeySecret = smsConfig.get("accessKeySecret");
        DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
        return new DefaultAcsClient(defaultProfile);
    }

    private CommonRequest getSmsCommonConfig(String providerId) {
        Map<String, String> config = getConfigDetail(SMS_COMMON_CONFIG.getName(),providerId);
        return getCommonRequest(config);
    }

    private Map<String, String> getConfigDetail(String configName, String providerId) {
        Map<String, String> result = configService.getConfig(configName,providerId);
        return result;
    }

    private CommonRequest getCommonRequest(Map<String, String> smsConfig) {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysMethod(MethodType.POST);
        String domain = smsConfig.get("domain");
        String product = smsConfig.get("product");
        String version = smsConfig.get("version");
        String action = smsConfig.get("action");
        commonRequest.setSysDomain(domain);
        commonRequest.setSysProduct(product);
        commonRequest.setSysVersion(version);
        commonRequest.setSysAction(action);
        return commonRequest;
    }

    public Integer getCourierCount(CourierVo courierVo) {
        return courierDao.getCourierCount(courierVo);
    }

    public Courier getCourier(String phone) {
        return courierDao.getCourier(phone);
    }

    public JSONObject getCounterNumber(String deviceId, String counterNumber) {
        return courierDao.getCounterNumber(deviceId,counterNumber);
    }

    public String findCounterNumber(String deviceId, String counterNumber) {
        return courierDao.findCounterNumber(deviceId,counterNumber);
    }
}
